package com.zl.controller;

import com.zl.common.Result;
import com.zl.dto.JournalDto;
import com.zl.entity.JournalEntity;
import com.zl.service.IJournalService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/journal")
public class JournalController {
    @Autowired
    private IJournalService journalService;




    @GetMapping("/list/{id}")
    public Result getById(@PathVariable Integer id){
        return Result.ok(journalService.getById(id));
    }
    @GetMapping("/list")
    public Result getAll(){
        List<JournalEntity> journalEntityList = journalService.listByIsDisplay();
        return Result.ok(journalEntityList);
    }

    @PostMapping("/save")
    public Result saveJournal(@RequestBody JournalDto journalDto){
        int res = journalService.insert(journalDto);
        if (res==0){
            return Result.fail("插入失败");
        }
        return Result.ok("插入成功");
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        boolean res = journalService.removeById(id);
        if (!res){
            return Result.fail("删除失败");
        }
        return Result.ok("删除成功");
    }

    @PutMapping("/update/{id}")
    public Result update(@RequestBody JournalDto journalDto,@PathVariable Integer id){


        JournalEntity journalEntity = new JournalEntity();
        BeanUtils.copyProperties(journalDto,journalEntity);
        boolean res = journalService.updateById(journalEntity);
        return res?Result.ok("修改成功"):Result.fail("修改失败");
    }
    @GetMapping("/select")
    public Result selectByCondition(@RequestParam(value = "title",defaultValue = "")String title,
                                    @RequestParam(value = "authors",defaultValue = "")String authors,
                                    @RequestParam(value = "pubDate",defaultValue = "")String pubDate){
        return journalService.selectByCondition(title,authors,pubDate);
    }


}
